{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8ecdcc23",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022/10/23/week 09\n",
    "* Python学习记录\n",
    "* 记录人：wu-huichun\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a050add2",
   "metadata": {},
   "source": [
    "# 元组（小括号）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21625987",
   "metadata": {},
   "source": [
    "### 一.元组和列表的比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1a5c0f8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表\n",
    "vowels1 = ['a','e','i','o','u']\n",
    "type(vowels1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3e55372f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组\n",
    "vowels2 = ('a','e','i','o','u')\n",
    "type(vowels2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de6dbb3c",
   "metadata": {},
   "source": [
    "### 二.元组是不可变的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5e1ecbbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'I', 'o', 'u']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表可变\n",
    "vowels1[2] = 'I'\n",
    "vowels1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3c219718",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [7]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 元组不可变\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m vowels2[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mI\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      3\u001b[0m vowels2\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# 元组不可变\n",
    "vowels2[2] = 'I'\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3a88a4c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'e', 'i', 'o', 'u')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59dd53c1",
   "metadata": {},
   "source": [
    "### 三.元组在小括号内至少要包含一个逗号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bfa83b49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('Python',)\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "56d16484",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Python',)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1ede5e5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
